SetGfxDriver 1:Graphics 1024,600,8,2 :SetBuffer FrontBuffer() :SeedRnd MilliSecs()

Global st1#,st2#,st3#,st4#,unbit$,fasecnt,maxfase1,minfase2,bit$,ty$,tyold$,tycnt,in$,in1,in2,in3,in4,in31,in41,in5,in6,in51,in61,in7,in8,in71,in81,in9,in10,in91,in101,in11,in12,in111,in121,in13,in14,untest,sort1,sort2,zzz1,hzzz1,jzzz1,yyyyy$,out$
in5 = CreateBank(6560*4):in6 = CreateBank(6560*4)
in1 = CreateBank(6560*4 ):in2 = CreateBank(6560*4):in3 = CreateBank(6560*4):in4 = CreateBank(6560*4):in31 = CreateBank(200):in41 = CreateBank(200)
Dim decode(4000):Dim encode1(1000000):Dim encode6(3400000)
Dim encode(4000):Dim aaa(60):Dim aaa1(60):Dim aaa2(60):sort1 = CreateBank(80):sort2 = CreateBank(60)
Dim zzz(66666):Dim zzz2(8881,100):Dim hzzz2(8881,100):Dim hzzz(66666):Dim jzzz2(8881,100):Dim jzzz(66666)
decodet():minfase2=800

st1#=1256:st2#=1256:st3#=2:st4#=2
While Not KeyHit(1) Or MouseHit(2)
ClsColor 0,0,0
Cls
randnum(0)
getnums()
;	fact_faza():erase()
;	Color 255,255,210:Text 5,5,bit+"/"+unbit+" "+fasecnt+":  "+ tycnt+" maxfase1: "+maxfase1+" minfase2: "+minfase2+"  middle=" + ttt0+"/"+ zzz1 + ", Null cnt=" + cnull+"%, max without Null="+wnullmax+", phase=3",False ,False
	
;Text 400,10,PeekByte 
it1=6:uu#=it1^11;Floor((Sin(it1*Pi)*Log(it1*Pi)*163*Cos(it1*14))^it1); Floor((it1^4)*(Sin(it1*14)^0.11)*10)^2+ Floor((it1^3)*(Cos(it1*14)^0.11))^3+ Floor((it1^3)*(Sin(it1*14)^0.11))^2 ;Floor((it1^Pi)*(Sin(it1*6)^0.97)*10)^Pi
Text 510,40,uu,False ,False

;checke()
;Color 210,255,210:Text 310,2,in,False ,False
zzz1=zzz1+1:hzzz1=hzzz1+1:jzzz1=jzzz1+1
Flip
If zzz1> 99    Then 
 WaitMouse()
;bit=WaitMouse()
If ((bit<>2) And (tycnt>maxfase1)) Then maxfase1=tycnt
If ((bit=2) And (tycnt<minfase2)) Then minfase2=tycnt	
bit=mod4(bit):
randnum(1):fasecnt=fasecnt+1
EndIf

VWait 
Wend 

Function erase()

		
End Function

Function fact_faza()



;If bit <>2 Then	
tt1=Rand(1,6)
tt2=Rand(1,6)
tt3=Rand(1,6)
tt4=Rand(1,6)
tt5=Rand(1,6)
tt6=Rand(1,6)


For it1=1 To 6
For t=0 To 728*4 Step 4:PokeInt in1,t,PeekInt(in2,t):Next	
;If (Int(it1/2))*2=it1 PokeInt in1,it1*36*9,mod1(PeekInt(in2,it1*36*9)+it1*1000) Else PokeInt in1,it1*36*9-36*9,mod1(PeekInt(in2,it1*36*9-36*9)+it1*1000)

;For g=0 To 35 Step 4
If (it1=1) Then PokeInt in1,it1*36*3,mod1(PeekInt(in2,it1*36*3)+it1*10000)
If (it1=2) Then PokeInt in1,it1*36*9,mod1(PeekInt(in2,it1*36*9)+it1*10000)
;Next

;	t1=0:For t=0 To 4782968*4 Step 36
;eee=1+fact(fact(PeekInt(in1,t+4*0),PeekInt(in1,t+4*4),PeekInt(in1,t+4*9)),fact(PeekInt(in1,t+4*1),PeekInt(in1,t+4*5),PeekInt(in1,t+4*10)),fact(PeekInt(in1,t+4*2),PeekInt(in1,t+4*6),PeekInt(in1,t+4*12)))
;	PokeInt in1,t1,eee:t1=t1+4
;	Next
	
;If (it1=2) Then PokeInt in1,it1*36,mod1(PeekInt(in1,it1*36)+it1*10000)
;	
;	t1=0:For t=0 To 531440*4 Step 36
;eee=1+fact(fact(PeekInt(in1,t+4*0),PeekInt(in1,t+4*4),PeekInt(in1,t+4*9)),fact(PeekInt(in1,t+4*1),PeekInt(in1,t+4*5),PeekInt(in1,t+4*10)),fact(PeekInt(in1,t+4*2),PeekInt(in1,t+4*6),PeekInt(in1,t+4*12)))
;	PokeInt in1,t1,eee:t1=t1+4
;	Next		
;
;if (it1=3) Then PokeInt in1,it1*36,mod1(PeekInt(in1,it1*36)+it1*10000)
;
;	t1=0:For t=0 To 59048*4 Step 36
;eee=1+fact(fact(PeekInt(in1,t+4*0),PeekInt(in1,t+4*4),PeekInt(in1,t+4*9)),fact(PeekInt(in1,t+4*1),PeekInt(in1,t+4*5),PeekInt(in1,t+4*10)),fact(PeekInt(in1,t+4*2),PeekInt(in1,t+4*6),PeekInt(in1,t+4*12)))
;	PokeInt in1,t1,eee:t1=t1+4
;	Next		

;If (it1=4) Then PokeInt in1,it1*36,mod1(PeekInt(in1,it1*36)+it1*10000)

;If (it1=5) Then PokeInt in1,it1*36,mod1(PeekInt(in1,it1*36)+it1*10000)

	t1=0:For t=0 To 728*4 Step 36
eee=1+fact(fact(PeekInt(in1,t+4*0),PeekInt(in1,t+4*4),PeekInt(in1,t+4*9)),fact(PeekInt(in1,t+4*1),PeekInt(in1,t+4*5),PeekInt(in1,t+4*10)),fact(PeekInt(in1,t+4*2),PeekInt(in1,t+4*6),PeekInt(in1,t+4*12)))
	PokeInt in1,t1,eee:t1=t1+4
	Next		

;If (it1=6) Then PokeInt in1,it1*36,mod1(PeekInt(in1,it1*36)+it1*10000)

	t1=0:For t=0 To 80*4 Step 36
eee=1+fact(fact(PeekInt(in1,t+4*0),PeekInt(in1,t+4*4),PeekInt(in1,t+4*9)),fact(PeekInt(in1,t+4*1),PeekInt(in1,t+4*5),PeekInt(in1,t+4*10)),fact(PeekInt(in1,t+4*2),PeekInt(in1,t+4*6),PeekInt(in1,t+4*12)))
	PokeInt in1,t1,eee:t1=t1+4
	Next		

t=0
eee1=1+fact(fact(PeekInt(in1,t+4*0),PeekInt(in1,t+4*4),PeekInt(in1,t+4*9)),fact(PeekInt(in1,t+4*1),PeekInt(in1,t+4*5),PeekInt(in1,t+4*10)),fact(PeekInt(in1,t+4*2),PeekInt(in1,t+4*6),PeekInt(in1,t+4*12)))

;Next
	
	
;For it2=1 To 6
For t=0 To 728*4 Step 4:PokeInt in3,t,PeekInt(in4,t):Next	
If (it1=3) Then  PokeInt in3,it1*36*3,mod1(PeekInt(in4,it1*36*3)+it1*100)
If (it1=4) Then  PokeInt in3,it1*36*9,mod1(PeekInt(in4,it1*36*9)+it1*100)
	
;If (it1=5) Then PokeInt in3,it1*36,mod1(PeekInt(in3,it1*36)+it1*10000)
	t1=0:For t=0 To 728*4 Step 36
eee=1+fact(fact(PeekInt(in3,t+4*0),PeekInt(in3,t+4*4),PeekInt(in3,t+4*9)),fact(PeekInt(in3,t+4*1),PeekInt(in3,t+4*5),PeekInt(in3,t+4*10)),fact(PeekInt(in3,t+4*2),PeekInt(in3,t+4*6),PeekInt(in3,t+4*12)))
	PokeInt in3,t1,eee:t1=t1+4
	Next		
;If (it1=6) Then PokeInt in3,it1*36,mod1(PeekInt(in3,it1*36)+it1*10000)
	t1=0:For t=0 To 80*4 Step 36
eee=1+fact(fact(PeekInt(in3,t+4*0),PeekInt(in3,t+4*4),PeekInt(in3,t+4*9)),fact(PeekInt(in3,t+4*1),PeekInt(in3,t+4*5),PeekInt(in3,t+4*10)),fact(PeekInt(in3,t+4*2),PeekInt(in3,t+4*6),PeekInt(in3,t+4*12)))
	PokeInt in3,t1,eee:t1=t1+4
	Next
t=0
eee2=1+fact(fact(PeekInt(in3,t+4*0),PeekInt(in3,t+4*4),PeekInt(in3,t+4*9)),fact(PeekInt(in3,t+4*1),PeekInt(in3,t+4*5),PeekInt(in3,t+4*10)),fact(PeekInt(in3,t+4*2),PeekInt(in3,t+4*6),PeekInt(in3,t+4*12)))





For t=0 To 728*4 Step 4:PokeInt in5,t,PeekInt(in6,t):Next	
If (it1=5) Then  PokeInt in5,it1*36*3,mod1(PeekInt(in6,it1*36*3)+it1*10090)
If (it1=6) Then  PokeInt in5,it1*36*9,mod1(PeekInt(in6,it1*36*9)+it1*10090)
		
;If (it1=5) Then PokeInt in5,it1*36,mod1(PeekInt(in5,it1*36)+it1*10000)
	t1=0:For t=0 To 728*4 Step 36
eee=1+fact(fact(PeekInt(in5,t+4*0),PeekInt(in5,t+4*4),PeekInt(in5,t+4*9)),fact(PeekInt(in5,t+4*1),PeekInt(in5,t+4*5),PeekInt(in5,t+4*10)),fact(PeekInt(in5,t+4*2),PeekInt(in5,t+4*6),PeekInt(in5,t+4*12)))
	PokeInt in5,t1,eee:t1=t1+4
	Next		
;If (it1=6) Then PokeInt in5,it1*36,mod1(PeekInt(in5,it1*36)+it1*10000)
	t1=0:For t=0 To 80*4 Step 36
eee=1+fact(fact(PeekInt(in5,t+4*0),PeekInt(in5,t+4*4),PeekInt(in5,t+4*9)),fact(PeekInt(in5,t+4*1),PeekInt(in5,t+4*5),PeekInt(in5,t+4*10)),fact(PeekInt(in5,t+4*2),PeekInt(in5,t+4*6),PeekInt(in5,t+4*12)))
	PokeInt in5,t1,eee:t1=t1+4
	Next
t=0
eee3=1+fact(fact(PeekInt(in5,t+4*0),PeekInt(in5,t+4*4),PeekInt(in5,t+4*9)),fact(PeekInt(in5,t+4*1),PeekInt(in5,t+4*5),PeekInt(in5,t+4*10)),fact(PeekInt(in5,t+4*2),PeekInt(in5,t+4*6),PeekInt(in5,t+4*12)))


;eee=1+fact( fact(eee1,fact(PeekInt(in2,0),PeekInt(in4,0),PeekInt(in6,0)),fact(PeekInt(in2,4),PeekInt(in4,4),PeekInt(in6,4))),fact(eee2,fact(PeekInt(in2,8),PeekInt(in4,8),PeekInt(in6,8)),fact(PeekInt(in2,12),PeekInt(in4,12),PeekInt(in6,12))),fact(eee3,fact(PeekInt(in2,16),PeekInt(in4,16),PeekInt(in6,16)),fact(PeekInt(in2,20),PeekInt(in4,20),PeekInt(in6,20))))
eee=1+fact(fact(fact(fact(fact(fact(eee1,eee2,eee3),fact(tt6,tt3,tt2),fact(tt4,tt5,tt2)),tt5,tt6),tt2,tt3),tt1,tt4),tt4,tt5)

		If it1= eee Then zzz(zzz1)=zzz(zzz1)+1:zzz2(zzz1,zzz2(zzz1,100))=it1:its=it1:zzz2(zzz1,100)=zzz2(zzz1,100)+1
		;If zzz2(zzz1,100)=0 Then 
		zzz2(zzz1,99)=eee:its=eee	


Next	
	



	
;	t=0
;		
;	If (zzz1>=0) And (zzz2(zzz1,100)<3) Then
;eee=1+fact(fact(PeekInt(in1,t+4*0),PeekInt(in1,t+4*4),PeekInt(in1,t+4*9)),fact(PeekInt(in1,t+4*1),PeekInt(in1,t+4*5),PeekInt(in1,t+4*10)),fact(PeekInt(in1,t+4*2),PeekInt(in1,t+4*6),PeekInt(in1,t+4*12)))
;For t=0 To 80*4 Step 4:PokeInt in1,t,PeekInt(in2,t):Next	
;PokeInt in2,0,mod1(PeekInt(in2,0)-eee)	
;	EndIf
	
	
	
	
Goto step1	
	For it1=1 To 6
For t=0 To 728*4 Step 4:PokeInt in1,t,PeekInt(in2,t):Next	
PokeInt in1,it1*36*9,mod1(PeekInt(in2,it1*36*9)+it1*1000)
PokeInt in1,it1*36*9,mod1(PeekInt(in1,it1*36*9)-it1*1000)	
	
;	t1=0:For t=0 To 6560*4 Step 36
;eee=1+fact(fact(PeekInt(in1,t+4*0),PeekInt(in1,t+4*4),PeekInt(in1,t+4*9)),fact(PeekInt(in1,t+4*1),PeekInt(in1,t+4*5),PeekInt(in1,t+4*10)),fact(PeekInt(in1,t+4*2),PeekInt(in1,t+4*6),PeekInt(in1,t+4*12)))
;	PokeInt in1,t1,eee:t1=t1+4
;	Next		

	t1=0:For t=0 To 728*4 Step 36
eee=1+fact(fact(PeekInt(in1,t+4*0),PeekInt(in1,t+4*4),PeekInt(in1,t+4*9)),fact(PeekInt(in1,t+4*1),PeekInt(in1,t+4*5),PeekInt(in1,t+4*10)),fact(PeekInt(in1,t+4*2),PeekInt(in1,t+4*6),PeekInt(in1,t+4*12)))
	PokeInt in1,t1,eee:t1=t1+4
	Next		

	t1=0:For t=0 To 80*4 Step 36
eee=1+fact(fact(PeekInt(in1,t+4*0),PeekInt(in1,t+4*4),PeekInt(in1,t+4*9)),fact(PeekInt(in1,t+4*1),PeekInt(in1,t+4*5),PeekInt(in1,t+4*10)),fact(PeekInt(in1,t+4*2),PeekInt(in1,t+4*6),PeekInt(in1,t+4*12)))
	PokeInt in1,t1,eee:t1=t1+4
	Next		
t=0
eee=1+fact(fact(PeekInt(in1,t+4*0),PeekInt(in1,t+4*4),PeekInt(in1,t+4*9)),fact(PeekInt(in1,t+4*1),PeekInt(in1,t+4*5),PeekInt(in1,t+4*10)),fact(PeekInt(in1,t+4*2),PeekInt(in1,t+4*6),PeekInt(in1,t+4*12)))


		If it1= eee Then jzzz(jzzz1)=jzzz(jzzz1)+1:jzzz2(jzzz1,jzzz2(jzzz1,100))=it1:jzzz2(jzzz1,100)=jzzz2(jzzz1,100)+1
		If jzzz2(jzzz1,100)=0 Then jzzz2(jzzz1,99)=eee

	Next	
	
.step1			
;Next					
				
;Else


	
;EndIf 	



End Function


























Function mod1(x)
While x>65535
 x = x -65536
Wend 
While  x<0 
 x = x +65536
Wend 
Return x
End Function

Function randnum(bt)

For i = 0 To 728*4 Step 4
t = Rand(0,65535):PokeInt in1,i, t:PokeInt in2,i, t
t = Rand(0,65535):PokeInt in3,i, t:PokeInt in4,i, t
t = Rand(0,65535):PokeInt in5,i, t:PokeInt in6,i, t
Next

If bt=1 Then
For i=0 To 66666
zzz1=0:jzzz1=0:hzzz1=0:tycnt=0
hzzz(i)=0:zzz(i)=0:jzzz(i)=0
Next
For i1=0 To 8881
For i2=0 To 100
jzzz2(i1,i2)=0
zzz2(i1,i2)=0
hzzz2(i1,i2)=0
Next
Next
EndIf

End Function


Function getnums()
	For y=0 To 2
	For x=0 To 2
		Color 0,255,0:Text 30+x*47,y*9+10,PeekInt(in1,(y*7+x)*4),False ,False 
	Next
	Next
;	xx1=fact(PeekInt(in1,0),PeekInt(in1,5),PeekInt(in1,10)):Text 150,10,xx1,False ,False 
;	xx2=fact(PeekInt(in1,0+15),PeekInt(in1,5+15),PeekInt(in1,10+15)):Text 150,19,xx2,False ,False 
;	xx3=fact(PeekInt(in1,0+2*15),PeekInt(in1,5+2*15),PeekInt(in1,10+2*15)):Text 150,28,xx3,False ,False 
;	lxx1 = fact(xx1,xx2,xx3)
	Text 160,22,lxx1,False ,False 
	
;	xx1=fact(PeekInt(in1,0),PeekInt(in1,0+15),PeekInt(in1,0+2*15)):Text 180,10,xx1,False ,False 
;	xx2=fact(PeekInt(in1,5),PeekInt(in1,5+15),PeekInt(in1,5+2*15)):Text 180,19,xx2,False ,False 
;	xx3=fact(PeekInt(in1,10),PeekInt(in1,10+15),PeekInt(in1,10+2*15)):Text 180,28,xx3,False ,False 
;	lxx2=fact(xx1,xx2,xx3)
	Text 190,22,lxx2,False ,False:Text 210,22,lxx1*6+lxx2,False ,False 

	fact_faza()
	erase()
	
	For y=0 To 2
	For x=0 To 2
	Color 255,255,0:Text 230+x*16,y*9+10,PeekByte(in2,(y*7+x)),False ,False 
	Next
	Next
;	xx1=fact(PeekInt(in2,0),PeekInt(in2,5),PeekInt(in2,10))
;	xx2=fact(PeekInt(in2,0+15),PeekInt(in2,5+15),PeekInt(in2,10+15))
;	xx3=fact(PeekInt(in2,0+2*15),PeekInt(in2,5+2*15),PeekInt(in2,10+2*15))
;	lxx1 = fact(xx1,xx2,xx3)
	Text 160,62,lxx1,False ,False 

;	xx1=fact(PeekInt(in2,0),PeekInt(in2,0+15),PeekInt(in2,0+2*15))
;	xx2=fact(PeekInt(in2,5),PeekInt(in2,5+15),PeekInt(in2,5+2*15))
;	xx3=fact(PeekInt(in2,10),PeekInt(in2,10+15),PeekInt(in2,10+2*15))
;	lxx2 = fact(xx1,xx2,xx3)
	Text 190,62,lxx2,False ,False :Text 210,62,lxx1*6+lxx2,False ,False 	
	
;         STAT
;	For i=0 To hzzz1
;		;If zzz(i) = 0 Then ttt0 = ttt0+1
;		ttt0 = ttt0+hzzz(i)
;		Color 155,255,0:Text 15+(i*19) Mod 960,180+68*Int(i/80),Str(hzzz(i))+",",False ,False 
;			For i9=0 To hzzz2(i,100)-1
;			Color hzzz2(i,i9)*21,hzzz2(i,i9)*51,255:Text 15+(i*19) Mod 960,180+68*Int(i/80)+i9*10+10,Str(hzzz2(i,i9))+"",False ,False 	
;			Next	
;		If hzzz2(i,100) = 0 Then 
;		Color 155,hzzz2(i,99)*51,hzzz2(i,99)*51:Text 15+(i*19) Mod 960,180+68*Int(i/80)-7,Str(hzzz2(i,99))+"",False ,False 	
;		EndIf 
;    Next
;
	For i=0 To jzzz1
		;If zzz(i) = 0 Then ttt0 = ttt0+1
		ttt0 = ttt0+jzzz(i)
		Color 105,255,0:Text 15+(i*9) Mod 960,155+68*Int(i/100),Str(jzzz(i))+",",False ,False 
			For i9=0 To jzzz2(i,100)-1
			Color jzzz2(i,i9)*21,jzzz2(i,i9)*51,155:Text 15+(i*9) Mod 960,155+68*Int(i/100)+i9*10+10,Str(jzzz2(i,i9))+"",False ,False 	
			Next	
		If jzzz2(i,100) = 0 Then 
		Color 105,jzzz2(i,99)*51,jzzz2(i,99)*51:Text 15+(i*9) Mod 960,155+68*Int(i/100)-7,Str(jzzz2(i,99))+"",False ,False 	
		EndIf 
    Next
	
	ttt0=0:wnullmax=0
	For i=0 To zzz1
		If zzz(i) = 0 Then 
			cnull = cnull +1
			If wnullmax < wnullmax0 Then wnullmax = wnullmax0
			wnullmax0=0
		EndIf
		If zzz(i) <> 0 Then wnullmax0=wnullmax0+1
		;If zzz(i) = 0 Then ttt0 = ttt0+1
		ttt0 = ttt0+zzz(i)
		Color 255,255,0:Text 15+(i*9) Mod 950,110+68*Int(i/100),Str(zzz(i))+",",False ,False 
			For i9=0 To zzz2(i,100)-1
			Color zzz2(i,i9)*51,zzz2(i,i9)*51,255:Text 15+(i*9) Mod 950,110+68*Int(i/100)+i9*10+10,Str(zzz2(i,i9))+"",False ,False 	
			Next	
		If zzz2(i,100) = 0 Then 
		Color 255,zzz2(i,99)*51,zzz2(i,99)*51:Text 15+(i*9) Mod 950,110+68*Int(i/100)-7,Str(zzz2(i,99))+"",False ,False 	
		EndIf 
    Next
	cnull = Ceil(cnull * 100 /  (zzz1+1))
	tnull = Ceil(tycnt* 100 /  (zzz1+1))

	Color 255,0,0:
	If bit=2 Text 140,89,"-",False ,False Else Color 0,255,0:Text 70,89,"+"

	Color 255,255,210:Text 10,95,tycnt+" maxfase1: "+maxfase1+" minfase2: "+minfase2+"  middle=" + ttt0+"/"+ zzz1 + ", One cnt=" + cnull+"%, max without Null="+wnullmax+", phase=4",False ,False

End Function


Function fact(x1,x2,x3)
        aaa(0)=x1+1:aaa(5)=x2+1:aaa(10)=x3+1
	For x=3 To 1 Step -1
	maxx =0
	For xt=0 To 2
	xx = aaa(xt*5):If maxx<xx Then maxx=xx:yy2=xt*5
	Next 
	aaa(yy2) =0:PokeByte sort1,yy2,x
	Next
	ttt = Int(""+ PeekByte (sort1,0*5) +""+PeekByte (sort1,1*5) +""+PeekByte (sort1,2*5))
	ttt=ttt+1
	xx = encode((ttt-1)*5)
	Return xx
End Function

Function fact6(x1,x2,x3,x4,x5,x6)
        aaa(0)=x1+1:aaa(5)=x2+1:aaa(10)=x3+1:aaa(15)=x4+1:aaa(20)=x5+1:aaa(25)=x6+1
	For x=6 To 1 Step -1
	maxx =0
	For xt=0 To 5
	xx = aaa(xt*5):If maxx<xx Then maxx=xx:yy2=xt*5
	Next 
	aaa(yy2) =0:PokeByte sort1,yy2,x
	Next
	ttt = Int(""+ PeekByte (sort1,0*5) +""+PeekByte (sort1,1*5) +""+PeekByte (sort1,2*5) +""+PeekByte (sort1,3*5) +""+PeekByte (sort1,4*5) +""+PeekByte (sort1,5*5) )
	ttt=ttt+1
	xx = encode6((ttt-1)*5)
	Return xx
End Function

Function mod4(x)
If x =2 Then x =1: Else x=2
Return x
End Function

Function decodet()
y=0
For x1=1 To 3
For x2=1 To 3
For x3=1 To 3
If((x1 <> x2) And (x1<>x3) And (x2<>x3)) decode(y) =x1+""+x2+""+x3:encode(Int(x1+""+x2+""+x3)*5)=y:y=y+1
Next
Next
Next


End Function